class GameScene extends BaseScene {

    //上级UI层
    frontUIContainer: FrontUIContainer;
    //上层特效层
    frontEffectContainer: egret.DisplayObjectContainer;

    btnGroup: eui.Group;
    //设置按钮
    btn_setting: ButtonEx;
    //退出按钮
    btn_quit: ButtonEx;

    //功能选择层
    funcSelectView: FuncSelectView;
    funcEffect: FuncEffectView;

    sceneCenter: GameSceneCenter;
    replayMenu: GameSceneReplayMenu;
    baoChange: GameSceneBaoChange;
    baoPai: GameSceneBao;

    img_state_2: eui.Image;
    img_state_3: eui.Image;
    img_state_4: eui.Image;

    startGroup: eui.Group;
    img_startbg: eui.Image;
    img_start: eui.Image;

    headViews: HeadIcon[];
    readyIcons: egret.Bitmap[];
    MJViews: GameSceneMJLayer[];

    face: IGameTapEvent;

    constructor(face: IGameTapEvent) {
        super();
        this.skinName = 'GameSceneSkin';

        this.face = face;
    }

    childrenCreated() {
        super.childrenCreated();

        // let rect: eui.Rect = new eui.Rect();
        // rect.width = this.width;
        // rect.height = this.height;
        // this.addChild(rect);

        this.horizontalCenter = this.verticalCenter = 0;

        this.MJViews = [];
        for (let i: number = 4; i >= 1; i--) {
            let mjView = new GameSceneMJLayer(i);
            this.addChild(mjView);

            this.MJViews[i] = mjView;
        }

        this.headViews = [];
        for (let i: number = 1; i <= 4; i++) {
            let headIcon = new HeadIcon(i);
            this.addChild(headIcon);

            this.headViews[i] = headIcon;
        }

        this.readyIcons = [];
        for (let i: number = 1; i <= 4; i++) {
            let readyIcon = new egret.Bitmap(RES.getRes("game_ready"));
            readyIcon.anchorOffsetX = readyIcon.width >> 1;
            readyIcon.anchorOffsetY = readyIcon.height >> 1;
            readyIcon.x = GSConfig.readyIconPos[i].x;
            readyIcon.y = GSConfig.readyIconPos[i].y;
            this.addChild(readyIcon);

            this.readyIcons[i] = readyIcon;
        }

        this.funcSelectView = new FuncSelectView;
        this.addChild(this.funcSelectView);

        this.frontUIContainer = new FrontUIContainer;
        this.addChild(this.frontUIContainer);

        this.frontEffectContainer = new egret.DisplayObjectContainer;
        this.addChild(this.frontEffectContainer);

        this.baoPai = new GameSceneBao;
        this.baoPai.x = 56;
        this.baoPai.y = 95;
        this.addChild(this.baoPai);

        this.baoPai.visible = GameConfig.hasBao;

        this.funcEffect = new FuncEffectView;
        this.frontEffectContainer.addChild(this.funcEffect);

        this.baoChange = new GameSceneBaoChange();

        this.replayMenu = new GameSceneReplayMenu;
        this.replayMenu.visible = false;
        this.addChild(this.replayMenu);

        this.startGroup.visible = false;

        this.btn_setting.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onSettingTap, this.face);
        this.btn_quit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onQuitTap, this.face);

        gameManager.i.addEventListener(GameEventType.GAME_START, this.gamestart, this);
    }

    private gamestart() {
        this.animation_start();
    }


    //更新房间信息
    updateRoom() {
        let head: HeadIcon;
        for (let i: number = 1; i <= 4; i++) {
            head = this.headViews[i];
            head.update(GSData.i.getRoomPlayerByDir(i));
            head.visible = PublicVal.i.getPos(i)
        }

        this.updateState();
    }

    updateState() {
        let img: eui.Image;
        for (let i: number = 2; i <= 4; i++) {
            img = this['img_state_' + i];
            if (img) {
                switch (game.status) {
                    case StateExType.changeThree:
                        img.texture = RES.getRes("img_txt_xuanpaizhong");
                        game.roomHuan[PublicVal.i.getPos(i)] && (img.visible = false);
                        break;
                    case StateExType.missing:
                        img.texture = RES.getRes("img_txt_dingquezhong");
                        break;
                    default:
                        img.visible = false;
                        break;
                }
            }
        }
    }

    //获取头像
    getHeadView(dir: number): HeadIcon {
        return this.headViews[dir];
    }


    //隐藏所有准备图标
    hideReadyIcons() {
        for (let i: number = 1; i <= 4; i++) {
            this.readyIcons[i].visible = false;
        }
    }

    //清理牌局麻将
    clearMJView() {
        for (let i: number = 1; i <= 4; i++) {
            let mjView: GameSceneMJLayer = this.MJViews[i];
            mjView.clear();
        }
        this.sceneCenter.reset();
    }


    //牌桌状态的头像重置
    readyStateHeadReset() {
        let headIcon: HeadIcon;
        for (let i: number = 1; i <= 4; i++) {
            headIcon = this.headViews[i];
            headIcon.skinState = 'intable';
            headIcon.x = GSConfig.headinitPos[i].x;
            headIcon.y = GSConfig.headinitPos[i].y;
            headIcon.reset();
        }
    }

    //牌局状态的头像重置
    playStateHeadReset() {
        let headIcon: HeadIcon;
        for (let i: number = 1; i <= 4; i++) {
            headIcon = this.headViews[i];
            headIcon.skinState = 'ingame';
            headIcon.x = GSConfig.headTargetPos[i].x;
            headIcon.y = GSConfig.headTargetPos[i].y;
        }
    }

    //播放功能特效
    playFuncEffect(dir: number, action: number) {
        this.funcEffect.play(dir, action);
    }

    //更新宝
    updateBao(pai: any) {
        this.baoPai.bao = pai;
    }

    //改变宝
    changeBao(pai: any = null) {
        this.baoChange.play(pai);
    }

    resetAllChildrenTouch() {
        let mjview: GameSceneMJLayer = this.MJViews[1];
        mjview.resetAllChildrenTouch();
    }

    //开场动画
    private animation_start() {
        this.startGroup.visible = true;
        this.img_start.alpha = 1;
        this.img_start.scaleX = this.img_start.scaleY = 1;

        let thiz = this;
        egret.Tween.get(this.img_start)
            .to({alpha: 0, scaleX: 2, scaleY: 2}, 300)
            .wait( 500)
            .call(function () {
                thiz.startGroup.visible = false;
            }, this);

        GameSound.play("sound_duijukaishi");
    }
}